﻿using System.Data;
using Microsoft.SharePoint;

namespace QueryDemo.ResultProviders
{
	/// <summary>
	/// SPSiteDataQuery to query site collection.
	/// </summary>
	public class SPSiteDataQueryResultProvider : BaseResultProvider
	{
		private SPSiteDataQuery _siteDataQuery;

		/// <summary>
		/// Sets properties related to use of the query within the web part.
		/// </summary>
		public SPSiteDataQueryResultProvider()
		{
			TypeOfQuery = QueryType.SPSiteDataQuery;
			QueryName = "SPSiteDataQuery";
		}

		public override bool CrossSiteCapable
		{
			get { return true; }
		}

		public override void Init()
		{
			_siteDataQuery = new SPSiteDataQuery
			                 	{
			                 		Webs = "<Webs Scope='SiteCollection'/>",
			                 		Lists = "<Lists ServerTemplate='" + ServerTemplate + "'/>",
			                 		ViewFields = GetViewFieldCaml(),
			                 		RowLimit = (uint) RowLimit
			                 	};
		}

		public override DataTable ExecuteQuery()
		{
			DataTable resultsTable = Web.GetSiteData(_siteDataQuery);
			return resultsTable;
		}
	}
}
